//
//  AppDelegate.m
//  Gift
//
//  Created by dllo on 15/9/10.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "AppDelegate.h"
#import "BaseHeader.h"
#import "ZWIntroductionViewController.h"
#import "LDViewController.h"
#import "LDSqlData.h"

@interface AppDelegate ()
@property (nonatomic, strong) ZWIntroductionViewController *introductionView;

@end

@implementation AppDelegate

- (void)dealloc
{
    [_window release];
    [super dealloc];
}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    //电池栏设置为白色
    [[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    if ([[user objectForKey:@"night"] isEqualToString:@"yes"]) {
        [[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleDefault];
            }
    
    [[LDSqlData sharedDataBase] openDB];  //打开数据库
    [[LDSqlData sharedDataBase] createTable];  //创建表结构
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    [_window release];
    
    GiftControllerd *gift = [[GiftControllerd alloc]init];
    UINavigationController *giftNav = [[UINavigationController alloc]initWithRootViewController:gift];
    UIImage *img1 = [UIImage imageNamed:@"1.png"];
    giftNav.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"礼物说" image:img1 tag:1000];
    
    
    
    
    
    SinglePieceControllerd * single = [[SinglePieceControllerd alloc]init];
    UINavigationController *singleNav = [[UINavigationController alloc]initWithRootViewController:single];
    UIImage *img2 = [UIImage imageNamed:@"2.png"];
    singleNav.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"单品" image:img2 tag:1001];
    
    ClassificationControllerd *classification = [[ClassificationControllerd alloc]init];
    UINavigationController *classificationNav = [[UINavigationController alloc]initWithRootViewController:classification];
    UIImage *img3 = [UIImage imageNamed:@"3.png"];
    classificationNav.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"分类" image:img3 tag:1002];
    
    
    LDArtifactController *Artifact= [[LDArtifactController alloc]init];
    UINavigationController *ArtifactNav = [[UINavigationController alloc]initWithRootViewController:Artifact];
    UIImage *img5 = [UIImage imageNamed:@"iconfont-shuidi"];//Artifact
    ArtifactNav.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"神器" image:img5 tag:1004];

    
    
    MyControllerd *my = [[MyControllerd alloc]init];
    UINavigationController *myNav = [[UINavigationController alloc]initWithRootViewController:my];
    UIImage *img4 = [UIImage imageNamed:@"4.png"];
    myNav.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"我" image:img4 tag:1003];
    
    
    
    
    
    
    UITabBarController *tabBarController = [[UITabBarController alloc]init];
    tabBarController.viewControllers = [NSArray arrayWithObjects:giftNav, singleNav, classificationNav, ArtifactNav, myNav, nil];
    UIImage * image = nil;
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    [tabBarController.tabBar setTintColor:[UIColor greenColor]];
    [self.window setRootViewController:tabBarController];
    
    
    [UMSocialData setAppKey:@"507fcab25270157b37000010"];
    
    [Artifact release];
    [ArtifactNav release];
    [single release];
    [singleNav release];
    [gift release];
    [giftNav release];
    [classification release];
    [classificationNav release];
    [my release];
    [myNav release];
    [giftNav.tabBarItem release];
    [singleNav.tabBarItem release];
    [classificationNav.tabBarItem release];
    [myNav.tabBarItem release];
    
    if ([self firstLoad]) {
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    LDViewController *mainVC = [[LDViewController alloc] init];
    self.window.rootViewController=mainVC;
    [_window makeKeyAndVisible];
    
    
    NSArray *coverImageNames = @[@"femalecodertocat", @"original", @"supportcat"];
    NSArray *backgroundImageNames = @[@"jingse", @"tangguo", @"tangguotow"];
    self.introductionView = [[ZWIntroductionViewController alloc] initWithCoverImageNames:coverImageNames backgroundImageNames:backgroundImageNames];
    
    
    [self.window addSubview:self.introductionView.view];
    
    AppDelegate *weakSelf = self;
    self.introductionView.didSelectedEnter = ^() {
        [weakSelf.introductionView.view removeFromSuperview];
        weakSelf.introductionView = nil;
        
        
        weakSelf.window.rootViewController = tabBarController;
        
    };

    }
    
    return YES;
    
}

- (BOOL)firstLoad {
    
    // 判断是否第一次使用这个版本
    NSString *key = (NSString *)kCFBundleVersionKey;
    // 获取上次的版本号
    NSString *lastVersion = [[NSUserDefaults standardUserDefaults] stringForKey:key];
    // 获取现在的版本号
    NSString *currentVersion = [NSBundle mainBundle].infoDictionary[key];
    // 判断2个版本号是否一致
    if ([lastVersion isEqualToString:currentVersion]) {
        // 一致,不显示新特性
        // 加载不是第一次使用的新特性
        return NO;
    } else {
        // 不一致,显示新特性(第一次使用软件)
        // 保存当前版本号
        [[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:key];
        [[NSUserDefaults standardUserDefaults] synchronize];
        
        return YES;
    }
    
}





- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
